package com.Washer;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.Result.BaseRespones;
import com.Result.StatusCode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class WasherOperation {
	/**
	 * @apiDefine Washer 洗衣机
	 */
	
	/**
	 * @api{Get} /Washer?method=getWasher 查询宿舍洗衣机情况
	 * @apiName getWasher
	 * @apiGroup Washer
	 * @apiDescription 根据指定字段查询或获取所有洗衣机信息（某个宿舍）
	 * @apiVersion 0.0.1
	 * 
	 * 
     * @apiParam {String} requestString 请求字段
     * 
	 * @apiSuccess {int} WasherID 洗衣机ID
	 * @apiSuccess {int} DormID 宿舍ID
	 * @apiSuccess {Boolean} IsFree 是否空闲 0被占 1空闲
	 * 
	 * @apiParamExample {json} 请求样例
	 *{ 
	 *  "DormID":10001
	 *}
	 * 
	 * @apiSuccessExample {json} 返回样例
	 *{'code':3050,
	 *'msg':'洗衣机信息查询成功',
	 *'timestamp':'2019-06-08 20:23:37',
	 *'data':
	 *[
	 *  {
	 *  'WasherID':10003,
	 *  'DormID':10001,
	 *  'IsFree':false
	 *  },
	 *  {
	 *  'WasherID':10001,
	 *  'DormID':10001,
	 *  'IsFree':true
	 *  },
	 *  {
	 *  'WasherID':10002,
	 *  'DormID':10001,
	 *  'IsFree':true
	 *  },
	 *  {
	 *  'WasherID':10004,
	 *  'DormID':10001,
	 *  'IsFree':true
	 *  },
	 *  {
	 *  'WasherID':10005,
	 *  'DormID':10001,
	 *  'IsFree':true
	 *  }
	 *  ]
	 *  }
	 */
	public static String getWasher(String requestString) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		
		BaseRespones response=null;
		WasherMapper mapper=(WasherMapper) session.getMapper(WasherMapper.class);
		System.out.println(requestString);
		
		List<Washer> washers;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); 
		
		Washer requestWasher=gson.fromJson(requestString, Washer.class);
		washers=mapper.getAllWasher(requestWasher.getDormID());
		if(washers.isEmpty()) {
			response = new BaseRespones(StatusCode.WasherInquireFail);
			return gson.toJson(response);
		}
		response=new BaseRespones(StatusCode.WasherInquireSuccess);
		response.setData(washers);
		return gson.toJson(response);
	}
	/**
	 * @apiDefine cargoUpdateErrorMsg 添加失败响应信息
	 * @apiError 110 未知错误
	 * @apiError 111 反序列化错误
	 * @apiError 112 序列化错误
	 * @apiError 3071 洗衣机信息添加失败
	 *
	 */

	/**
	 * @apiDefine cargoUpdateSuccessMsg 更新成功响应信息
	 * @apiSuccess {int} code 响应码
	 * @apiSuccess {String} msg 响应信息
	 * @apiSuccess {timestamp} timestamp 时间戳
	 */
	
	
	/**
	 * @api{Post} /Washer?method=insertWasher 增加洗衣机
	 * @apiName insertWasher
	 * @apiGroup Washer
	 * @apiDescription 商家添加洗衣机信息 在洗衣机对象中给需要的字段赋值
	 * @apiVersion 0.0.1
	 * 
	 * @apiParamExample {json} 请求样例
	 *{ 
	 *"DormID":10005,
	 *"IsFree":true
	 *}
	 * 
	 * @apiUse cargoUpdateErrorMsg
	 * @apiUse cargoUpdateSuccessMsg
	 * 
	 * @apiSuccessExample 
	 * Success-Response: 
	 *{
	 * 'code':3050,
	 * 'msg':'洗衣机信息查询成功',
	 * 'timestamp':'2019-06-08 20:52:44'
	 *}
	 */
	public static String insertWasher(String requestString) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		
		BaseRespones response=null;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();  
		Washer washer=null;
		
		try {
			washer = gson.fromJson(requestString, Washer.class);
		} catch (Exception e) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}

		if (washer==null){
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		
		
		// washer mapper
		WasherMapper washerMapper=(WasherMapper)session.getMapper(WasherMapper.class);
		System.out.println(gson.toJson(washer));
		// 执行更新
		int rs=washerMapper.insertWasher(washer);
		if (rs==0) {
			response = new BaseRespones(StatusCode.WasherInsertFail);
			return gson.toJson(response);
		}
		session.commit();
		session.close();
		response = new BaseRespones(StatusCode.WasherInsertSuccess);
		return gson.toJson(response);	
	}


}
